﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;

namespace BTS10_To_BTS13_Party_Converter
{
    class PartyConfig
    {
        public XmlDocument migratetoBTS13(XmlDocument xDoc)
        {
            AgreementHandler handler = new AgreementHandler();
            XmlNodeList xmlX12ValOverrideNodes = null;
            XmlNodeList xmlPartnershipNodes = xDoc.SelectNodes(Constants.partnershipXpath);
            int iPartnerShip = 1;
            int iAgreement = 1;

            foreach (XmlNode node in xmlPartnershipNodes)
            {
                XmlNodeList xmlAgreementNodes = xDoc.SelectNodes(Constants.partnershipXpath +"[" + iPartnerShip + "]"+ Constants.agreementXpath);
                
                foreach (XmlNode agNode in xmlAgreementNodes)
                {
                    handler.ModifyX12(agNode, "OnewayAgreementAtoB");
                    xmlX12ValOverrideNodes = xDoc.SelectNodes(Constants.partnershipXpath + "[" + iPartnerShip + "]" + Constants.agreementXpath + "[" + iAgreement + "]" + Constants.AtoBX12Xpath);
                    foreach (XmlNode x12Node in xmlX12ValOverrideNodes)
                    {
                        handler.ModifyProtocolSettings(x12Node);
                    }
                    handler.ModifyX12(agNode, "OnewayAgreementBtoA");
                    xmlX12ValOverrideNodes = xDoc.SelectNodes(Constants.partnershipXpath + "[" + iPartnerShip + "]" + Constants.agreementXpath + "[" + iAgreement + "]" + Constants.BtoAX12Xpath);
                    foreach (XmlNode x12Node in xmlX12ValOverrideNodes)
                    {
                        handler.ModifyProtocolSettings(x12Node);
                    }

                    iAgreement = iAgreement + 1;
                }
                iAgreement = 1;
                iPartnerShip = iPartnerShip + 1;
            }
            return xDoc;
        }
    }
}
